﻿using System.Linq;
using MvcDesign.Core.CoreDataModel;

namespace MvcDesign.Core.CoreServices
{
    public class Configuration : IConfiguration
    {
        private static string GetItem(string key)
        {
            using (var domainModel = new DataAccess.DataContext())
            {
                return domainModel.GetCollection<ConfigurationItem>().Where(c => c.Id == key).Select(c => c.Value).FirstOrDefault();
            }
        }

        private static void SetItem(string key, string value)
        {
            using (var domainModel = new DataAccess.DataContext())
            {
                var configurationItem = domainModel.GetCollection<ConfigurationItem>().FirstOrDefault(c => c.Id == key);
                if (configurationItem == null)
                {
                    configurationItem = domainModel.GetCollection<ConfigurationItem>().Create();
                    configurationItem.Id = key;
                }
                configurationItem.Value = value;
                domainModel.SaveChanges();
            }
        }

        public string ForumName
        {
            get { return GetItem("Name"); }
            set { SetItem("Name", value); }
        }

        public string ForumDescription
        {
            get { return GetItem("Description"); }
            set { SetItem("Description", value); }
        }
    }
}
